import 'package:chewie/chewie.dart';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../helper/uidata.dart';
import 'package:video_player/video_player.dart';

class MoviePlayPage extends StatefulWidget {
  MoviePlayPage(this.guid, {this.title = '视频播放'});
  final String title;
  final String guid;

  @override
  State<StatefulWidget> createState() {
    return _MoviePlayPageState();
  }
}

class _MoviePlayPageState extends State<MoviePlayPage> {
  late VideoPlayerController _videoPlayerController1;
  late ChewieController _chewieController;

  @override
  void initState() {
    super.initState();
    this.initializePlayer();
  }

  @override
  void dispose() {
    _videoPlayerController1.dispose();

    _chewieController.dispose();
    super.dispose();
  }

  Future<void> initializePlayer() async {
    _videoPlayerController1 =
        VideoPlayerController.network(UIData.imageDownloadServer + widget.guid);

    print(UIData.imageDownloadServer + widget.guid);
    await _videoPlayerController1.initialize();
    _chewieController = ChewieController(
      videoPlayerController: _videoPlayerController1,
      autoPlay: false,
      looping: true,
      // Try playing around with some of these other options:
      // showControls: false,
      // materialProgressColors: ChewieProgressColors(
      //   playedColor: Colors.red,
      //   handleColor: Colors.blue,
      //   backgroundColor: Colors.grey,
      //   bufferedColor: Colors.lightGreen,
      // ),
      // placeholder: Container(
      //   color: Colors.grey,
      // ),
      // autoInitialize: true,
    );
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: _chewieController != null
          ? Chewie(
              controller: _chewieController,
            )
          : Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                CircularProgressIndicator(),
                SizedBox(height: 20),
                Text('Loading'),
              ],
            ),

      // ElevatedButton(
      //   onPressed: () {
      //     _chewieController.enterFullScreen();
      //   },
      //   child: Text('Fullscreen'),
      // ),
    );
  }
}
